﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using OctoOS.UI;
using OctoOS;


public partial class root_admin_adsApprove : OOSPage
{
    class PathInfo
    {
        [PathInfoMember(0)]
        internal int pageIndex;

        internal void PostInit()
        {
            if (pageIndex > 0)
            {
                pageIndex--;
            }
        }
    }


    PathInfo _pathInfo;
    int _adsOnPage = 2;
    int _startIndex;
    ClassifiedAd[] _ads;
    Category[] _allCategories = new Category[] { };

    protected void Page_Init(object sender, EventArgs e)
    {
#warning admin
        //OOSRequest.Current.EnsureModerator();
        _pathInfo = EnablePathInfo<PathInfo>();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        PageTitle = "Ads to approve";

        AddBreadcrumb(string.Empty, "/");
        AddBreadcrumb("Admin page", Urls.Classifieds.GetAdminDefault());

        _startIndex = _pathInfo.pageIndex * _adsOnPage;
        int totalAds;
        _ads = DataSource.ClassifiedAds_GetByStatus(
            OOSRequest.Current.ResourceID,
            (byte)Helper.AdStatus.Pending,
            _startIndex,
            _adsOnPage,
            out totalAds);
        if (_ads.Length > 0)
        {
            int pageMaxCnt = Convert.ToInt32(Math.Ceiling((double)totalAds / _adsOnPage));
            adsNumericPager.PageCount = pageMaxCnt;
            adsNumericPager.SelectedIndex = _pathInfo.pageIndex;
            adsNumericPager.PageUrlPattern = "/root/admin/adsApproveList.aspx/{0}";
            adsNumericPager.FirstPageUrl = Urls.Classifieds.GetAdsToApproveList(0);

            Helper.Categories.UpdateCategoryCache();
            _allCategories = Helper.Categories.GetAllCategories();
            ShowFoundAds(_ads);
        }
        else
        {
            Msg.Visible = true;
        }
    }

    private void ShowFoundAds(ClassifiedAd[] ads)
    {
        foreach (var ad in ads)
        {
            OctoOS.UI.ListViewItem row = new OctoOS.UI.ListViewItem();
            row.Controls.Add(GetObjectPreview(ad));
            ListView.Controls.Add(row);
        }
    }

    private ObjectPreview GetObjectPreview(ClassifiedAd ad)
    {
        Image48ObjectPreview pr = new Image48ObjectPreview();
        pr.ImageUrl = "/Images/email_icon.jpg";
        pr.HeaderText = ad.Title;
        pr.HeaderUrl = Urls.Classifieds.GetAdApprove(ad.ID);
        pr.Footer = new OctoOS.UI.Literal(GetCategoriesFooter(ad.CategoryID));
        pr.Body = new OctoOS.UI.Literal("Post Time: " + ad.PostTime.ToShortDateString() + ", " + ad.PostTime.ToShortTimeString());
        return pr;
    }

    private string GetCategoriesFooter(long categoryID)
    {
        string result = "";
        Category grandParentCategory = Helper.Categories.GetGrandParentCategory(categoryID);
        Category parentCategory = Helper.Categories.GetParentCategory(categoryID);
        Category category = Helper.Categories.GetCategory(categoryID);
        if (category != null)
        {
            result = category.Name;
        }
        if (parentCategory!= null)
        {
            result = parentCategory.Name + " -> " + result;
            if (grandParentCategory!= null)
            {
                result = grandParentCategory.Name + " -> " + result;
            }
        }
        return "Category: " + result;
    }
}